package com.guangaiboen.attendance.core.dispatcher.utils;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;

/**
 * 判断对象是否为空或null
 */
public class ObjectUtils extends org.apache.commons.lang3.ObjectUtils {

    public static boolean isNull(Object obj) {
        return obj == null;
    }

    public static boolean isNotNull(Object obj) {
        return !isNull(obj);
    }

    public static boolean isEmpty(Object obj) {
        if (obj == null) return true;
        else if (obj instanceof CharSequence) return ((CharSequence) obj).length() == 0;
        else if (obj instanceof Collection) return ((Collection) obj).isEmpty();
        else if (obj instanceof Map) return ((Map) obj).isEmpty();
        else if (obj.getClass().isArray()) return Array.getLength(obj) == 0;

        return false;
    }

    public static boolean isNotEmpty(Object obj) {
        return !isEmpty(obj);
    }

    /**
     * 功能类似三目运算符
     * @param value
     * @param defaultValue
     * @param <T>
     * @return
     */
    public static <T> T getOrDefault(T value,T defaultValue){
        if(value == null){
            return defaultValue;
        }
        return value;
    }
}
